package com.cyy.springbootinit.service.impl;
import java.util.Date;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.springbootinit.common.ErrorCode;
import com.cyy.springbootinit.constant.CommonConstant;
import com.cyy.springbootinit.exception.ThrowUtils;
import com.cyy.springbootinit.mapper.DormitoryMapper;
import com.cyy.springbootinit.model.dto.dorimitory.DormitoryQueryRequest;
import com.cyy.springbootinit.model.entity.Building;
import com.cyy.springbootinit.model.entity.Dormitory;

import com.cyy.springbootinit.model.entity.User;
import com.cyy.springbootinit.model.vo.DormitoryVO;
import com.cyy.springbootinit.model.vo.UserVO;
import com.cyy.springbootinit.service.BuildingService;
import com.cyy.springbootinit.service.DormitoryService;
import com.cyy.springbootinit.service.UserService;
import com.cyy.springbootinit.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 宿舍服务实现
 *
 * @author 程序员cyy</a>
 * @from cyy</a>
 */
@Service
@Slf4j
public class DormitoryServiceImpl extends ServiceImpl<DormitoryMapper, Dormitory> implements DormitoryService {

    @Resource
    private BuildingService buildingService;

    /**
     * 校验数据
     *
     * @param dormitory
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validDormitory(Dormitory dormitory, boolean add) {

        ThrowUtils.throwIf(dormitory == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值
        String dormitoryName = dormitory.getDormitoryName();
        System.out.println("dormitoryName"+dormitoryName);
        Integer buildingId = dormitory.getBuildingId();
        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则
            ThrowUtils.throwIf(buildingId==null, ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(!StringUtils.isNotBlank(dormitoryName), ErrorCode.PARAMS_ERROR, "楼宇名称不能为空");
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则
    }

    /**
     * 获取查询条件
     *
     * @param dormitoryQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Dormitory> getQueryWrapper(DormitoryQueryRequest dormitoryQueryRequest) {
        QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
        if (dormitoryQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Integer dormitoryId = dormitoryQueryRequest.getDormitoryId();
        String dormitoryName = dormitoryQueryRequest.getDormitoryName();
        Integer buildingId = dormitoryQueryRequest.getBuildingId();
        String searchText = dormitoryQueryRequest.getSearchText();
        String sortField = dormitoryQueryRequest.getSortField();
        String sortOrder = dormitoryQueryRequest.getSortOrder();
        // todo 补充需要的查询条件
        // 从多字段中搜索
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(dormitoryName), "dormitoryName", dormitoryName);
        // JSON 数组查询
        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(dormitoryId), "dormitoryId", dormitoryId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(buildingId), "buildingId", buildingId);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取宿舍封装
     *
     * @param dormitory
     * @param request
     * @return
     */
    @Override
    public DormitoryVO getDormitoryVO(Dormitory dormitory, HttpServletRequest request) {
        if(dormitory==null) return null;
        // 对象转封装类
        DormitoryVO dormitoryVO = DormitoryVO.objToVo(dormitory);
        QueryWrapper<Building> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("buildingId",dormitoryVO.getBuildingId());
        //System.out.println("dorimitoryVO"+dormitoryVO);
        //.out.println("dormitoryVO.getBuildingId()"+dormitoryVO.getBuildingId());
        dormitoryVO.setBuilding(buildingService.getBuildingVO(buildingService.getOne(queryWrapper),request));
        return dormitoryVO;
    }

    /**
     * 分页获取宿舍封装
     *
     * @param dormitoryPage
     * @param request
     * @return
     */
    @Override
    public Page<DormitoryVO> getDormitoryVOPage(Page<Dormitory> dormitoryPage, HttpServletRequest request) {
        List<Dormitory> dormitoryList = dormitoryPage.getRecords();
        Page<DormitoryVO> dormitoryVOPage = new Page<>(dormitoryPage.getCurrent(), dormitoryPage.getSize(), dormitoryPage.getTotal());
        if (CollUtil.isEmpty(dormitoryList)) {
            return dormitoryVOPage;
        }
        // 对象列表 => 封装对象列表
        List<DormitoryVO> dormitoryVOList = dormitoryList.stream().map(dormitory -> {
            return DormitoryVO.objToVo(dormitory);
        }).collect(Collectors.toList());


        dormitoryVOList.forEach(dormitoryVO -> {
            QueryWrapper<Building> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("building_id",dormitoryVO.getBuildingId());
            dormitoryVO.setBuilding(buildingService.getBuildingVO(buildingService.getOne(queryWrapper),request));
        });
        // endregion

        dormitoryVOPage.setRecords(dormitoryVOList);
        return dormitoryVOPage;
    }

}
